package com.cff.util;

import java.io.UnsupportedEncodingException;

import org.apache.commons.lang3.StringUtils;

/**
 * @version 1.0
 * @since 1.0
 */
public class StringUtil {
	private static final String CHARSET_NAME = "UTF-8";

	public static boolean isEmpty(String str) {
		return StringUtils.isEmpty(str);
	}

	public static boolean isNotEmpty(String str) {
		return StringUtils.isNotEmpty(str);
	}

	public static String trim(String str) {
		return StringUtils.trim(str);
	}

	public static String trimToNull(String str) {
		return StringUtils.trimToNull(str);
	}

	public static String trimToEmpty(String str) {
		return StringUtils.trimToEmpty(str);
	}

	public static boolean equals(String str1, String str2) {
		return StringUtils.equals(str1, str2);
	}

	public static boolean equalsIgnoreCase(String str1, String str2) {
		return StringUtils.equalsIgnoreCase(str1, str2);
	}

	public static int indexOf(String str, String searchStr) {
		return StringUtils.indexOf(str, searchStr);
	}

	public static boolean contains(String str, String searchStr) {
		return StringUtils.contains(str, searchStr);
	}

	public static String substring(String str, int start) {
		return StringUtils.substring(str, start);
	}

	public static String substring(String str, int start, int end) {
		return StringUtils.substring(str, start, end);
	}

	public static String replace(String text, String repl, String with) {
		if (text == null || repl == null || repl.length() == 0
				|| text.indexOf(repl) == -1) {
			return text;
		}
		return StringUtils.replace(text, repl, with);
	}

	public static String betweenReplace(String text, String boxString,
			String with) {
		if (text == null || boxString == null || with == null
				|| boxString.length() == 0) {
			return text;
		}

		StringBuffer buf = new StringBuffer(text.length());
		int start = 0, end = 0;
		while ((end = text.indexOf(boxString, start)) != -1) {
			buf.append(text.substring(start, end));
			buf.append(with);
			start = end + boxString.length();
			end = text.indexOf(boxString, start);
			if (end == -1) {
				return buf.toString();
			}
			start = end + boxString.length();
		}
		buf.append(text.substring(start));
		return buf.toString();
	}

	public static String repeat(String str, int repeat) {
		return StringUtils.repeat(str, repeat);
	}

	public static String right(String str, int len) {
		return StringUtils.right(str, len);
	}

	public static String left(String str, int len) {
		return StringUtils.left(str, len);
	}

	public static String leftB(String str, int len) {
		if (str == null) {
			return null;
		}
		if (len <= 0) {
			return "";
		}
		try {
			if (getByteLength(str) <= len) {
				return str;
			} else {
				String newStr = new String(str.getBytes(CHARSET_NAME), 0, len,
						CHARSET_NAME);
				if (equals(newStr, "")) {
					return new String(str.getBytes(CHARSET_NAME), 0, len - 1,
							CHARSET_NAME);
				} else {
					return newStr;
				}
			}
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	public static String rightPad(String str, int size) {
		return StringUtils.rightPad(str, size);
	}

	public static String rightPad(String str, int size, String delim) {
		return StringUtils.rightPad(str, size, delim);
	}

	public static String leftPad(String str, int size) {
		return StringUtils.leftPad(str, size);
	}

	public static String leftPad(String str, int size, String delim) {
		return StringUtils.leftPad(str, size, delim);
	}

	public static String center(String str, int size) {
		return StringUtils.center(str, size);
	}

	public static String center(String str, int size, String delim) {
		return StringUtils.center(str, size, delim);
	}

	public static String upperCase(String str) {
		return StringUtils.upperCase(str);
	}

	public static String lowerCase(String str) {
		return StringUtils.lowerCase(str);
	}

	public static String capitalize(String str) {
		return StringUtils.capitalize(str);
	}

	public static String uncapitalize(String str) {
		return StringUtils.uncapitalize(str);
	}

	public static String swapCase(String str) {
		return StringUtils.swapCase(str);
	}

	public static final String toUpperCaseTop(String str) {
		if (str == null) {
			return null;
		}
		if (str.equals("")) {
			return "";
		}
		StringBuffer newName = new StringBuffer("");
		newName.append(str.toUpperCase().charAt(0));
		newName.append(str.substring(1));
		return newName.toString();
	}

	public static final String toLowerCaseTop(String str) {
		if (str == null) {
			return null;
		}
		if (str.equals("")) {
			return "";
		}
		StringBuffer newName = new StringBuffer("");
		newName.append(str.toLowerCase().charAt(0));
		newName.append(str.substring(1));
		return newName.toString();
	}

	public static final String[] split(String str) {
		return StringUtils.split(str);
	}

	public static final String[] split(String str, String separatorChars) {
		return StringUtils.split(str, separatorChars);
	}

	public static final String join(Object[] array) {
		return StringUtils.join(array);
	}

	public static final String join(Object[] array, String separator) {
		return StringUtils.join(array, separator);
	}

	public static int getByteLength(String value) {
		if (value == null) {
			return 0;
		}
		try {
			return value.getBytes(CHARSET_NAME).length;
		} catch (UnsupportedEncodingException e) {
			return 0;
		}
	}
	
    /**
     * <p>Returns either the passed in String,
     * or if the String is {@code null}, an empty String ("").</p>
     *
     * <pre>
     * StringUtil.defaultString(null)  = ""
     * StringUtil.defaultString("")    = ""
     * StringUtil.defaultString("bat") = "bat"
     * </pre>
     *
     * @see {@link StringUtils#defaultString(String)}}
     * @param str  the String to check, may be null
     * @return the passed in String, or the empty String if it
     *  was {@code null}
     */
    public static String defaultString(String str) {
    	return StringUtils.defaultString(str);
    }

    /**
     * <p>Returns either the passed in String, or if the String is
     * {@code null}, the value of {@code defaultStr}.</p>
     *
     * <pre>
     * StringUtil.defaultString(null, "NULL")  = "NULL"
     * StringUtil.defaultString("", "NULL")    = ""
     * StringUtil.defaultString("bat", "NULL") = "bat"
     * </pre>
     *
     * @see {@link StringUtils#defaultString(String, String)}}
     * @param str  the String to check, may be null
     * @param defaultStr  the default String to return
     *  if the input is {@code null}, may be null
     * @return the passed in String, or the default if it was {@code null}
     */
	public static String defaultString(String value, String defaultString) {
		return StringUtils.defaultString(value, defaultString);
	}

    /**
     * <p>Returns either the passed in CharSequence, or if the CharSequence is
     * whitespace, empty ("") or {@code null}, the value of {@code defaultStr}.</p>
     *
     * <pre>
     * StringUtils.defaultIfBlank(null, "NULL")  = "NULL"
     * StringUtils.defaultIfBlank("", "NULL")    = "NULL"
     * StringUtils.defaultIfBlank(" ", "NULL")   = "NULL"
     * StringUtils.defaultIfBlank("bat", "NULL") = "bat"
     * StringUtils.defaultIfBlank("", null)      = null
     * </pre>
     * @param <T> the specific kind of CharSequence
     * @param str the CharSequence to check, may be null
     * @param defaultStr  the default CharSequence to return
     *  if the input is whitespace, empty ("") or {@code null}, may be null
     * @return the passed in CharSequence, or the default
     * @see org.apache.commons.lang3.StringUtils#defaultIfBlank(CharSequence, CharSequence)
     */
    public static <T extends CharSequence> T defaultIfBlank(T str, T defaultStr) {
        return StringUtils.defaultIfBlank(str, defaultStr);
    }

    /**
     * <p>Returns either the passed in CharSequence, or if the CharSequence is
     * empty or {@code null}, the value of {@code defaultStr}.</p>
     *
     * <pre>
     * StringUtils.defaultIfEmpty(null, "NULL")  = "NULL"
     * StringUtils.defaultIfEmpty("", "NULL")    = "NULL"
     * StringUtils.defaultIfEmpty(" ", "NULL")   = " "
     * StringUtils.defaultIfEmpty("bat", "NULL") = "bat"
     * StringUtils.defaultIfEmpty("", null)      = null
     * </pre>
     * @param <T> the specific kind of CharSequence
     * @param str  the CharSequence to check, may be null
     * @param defaultStr  the default CharSequence to return
     *  if the input is empty ("") or {@code null}, may be null
     * @return the passed in CharSequence, or the default
     * @see org.apache.commons.lang3.StringUtils#defaultIfEmpty(CharSequence, CharSequence)
     */
    public static <T extends CharSequence> T defaultIfEmpty(T str, T defaultStr) {
        return StringUtils.defaultIfEmpty(str, defaultStr);
    }
	public static String rtrim(String value) {
		if (value == null) {
			return null;
		}
		return value.replaceAll("\\s+$", "");
	}

	public static String ltrim(String value) {
		if (value == null) {
			return null;
		}
		return value.replaceAll("^\\s+", "");
	}

	/**
	 * @param str
	 * @return
	 */
	public static String stringToBytes(String str) {
		byte[] b = null;
		try {
			b = str.getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
		}
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < b.length; i++) {
			sb.append(b[i]).append("/");
		}
		return sb.toString();
	}

}
